/**
 * Created by yangfan on 2016/8/12.
 */
define(["config/routermodel", 'angular',"ngSanitize", 'router','ngCookies'], function (routermodel) {
    var app = angular.module("myModule", ['ui.router',"ngSanitize","ngCookies"]);
    app.config(function ($controllerProvider, $compileProvider, $filterProvider, $provide, $stateProvider,$urlRouterProvider) {
        app.register = {
            controller: $controllerProvider.register,
            directive: $compileProvider.directive,
            filter: $filterProvider.register,
            service: $provide.service,
            factory: $provide.factory,
            stateProvider: $stateProvider,
            urlRouterProvider:$urlRouterProvider
        };
    }).config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $urlRouterProvider.otherwise('indexCtrl');
        var model = routermodel;
        var index = 0;
        function routerSet(){
            //循环调用 实现将model中所有设置好的对象 通过 $stateProvide.state（）一一配置
            var item = model[index];
            if (!angular.isString(item.state) || !angular.isString(item.url) || !angular.isString(item.url)) {
                return
            }
            var strurl = item.ctrl;
            //var ctrlName = strurl.substring(strurl.lastIndexOf('/')+1);
            var ctrlName = item.ctrlName;

            //配置子路由url
            var fatherState, stateUrl = item.state;

            var abstract  = false ;
            if(item.state.indexOf(".")!=-1){
                fatherState = item.state.substring(0,item.state.indexOf('.'));
                stateUrl = item.state.substring(item.state.indexOf('.')+1);
               // var stateUrl  = item.state.replace(".","/");
            }else{
                fatherState = item.state
            }
            ////针对父子路由做分级
            //if(fatherState.indexOf('.')>-1){
            //    fatherState = fatherState.replace('.','/')
            //    if(fatherState.indexOf(':')>-1){
            //        fatherState = fatherState.replace(':','/:')
            //    }
            //}
            //if(stateUrl.indexOf('.')>-1){
            //    stateUrl = stateUrl.replace('.','/');
            //    if(stateUrl.indexOf(':')>-1){
            //        stateUrl = stateUrl.replace(':','/:')
            //    }
            //}
            //if(item.state.indexOf(':')!= -1){
            //    fatherState = item.state.substring(0,item.state.indexOf('.'));
            //    stateUrl = item.state.substring(item.state.lastIndexOf('.')+1);
            //}

                //  待优化...
            //var arrTemp= [],stateName,urlName ;
            //if(item.state.indexOf(".")!=-1){
            //    //stateUrl = item.state.substring(item.state.lastIndexOf('.')+1);
            //    //stateUrl  = item.state.replace(".","/");
            //    arrTemp  = item.state.split(".");
            //    stateName = arrTemp[arrTemp.length-2];
            //    urlName = (arrTemp.splice(1,arrTemp.length-1)).join("/");
            //
            //}else{
            //    stateName = item.state ;
            //}
            if(item.hasOwnProperty("params")){
                var paramsTemp = [],paramsStr;
                for(var key in item.params){
                    paramsTemp.push(item.params[key]);
                }
                paramsStr = "?" + paramsTemp.join("&");
                stateUrl = stateUrl + paramsStr ;
            }

            if(item.hasOwnProperty("config")){
                if(typeof (item.config.abstract) == "undefined")
                return
                if(typeof (item.config.abstract)=="boolean"){
                    abstract = item.config.abstract
                }
            }
            $stateProvider.state(item.state, {
                url:"/"+ stateUrl,
                cache:false,
                controller: ctrlName,
                templateUrl: item.url,
                abstract:abstract,
                resolve: {
                    loadCtrl: ["$q", function($q) {
                        var deferred = $q.defer();
                        //异步加载controller／directive/filter/service
                        require([
                            item.ctrl
                        ], function() { deferred.resolve(); });
                        return deferred.promise;
                    }]
                }
            });
            index++;
            if(index <model.length){
                arguments.callee ();
            }
        }
        routerSet();
    }]);

    return app;
})